package com.element.music.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.element.music.component.util.FileUtils;
import com.element.music.controller.vo.UserIdVO;
import com.element.music.repository.entity.user.UserEntity;
import com.element.music.repository.repository.UserRepository;
import com.element.music.service.UserService;
import com.element.music.service.converter.UserConverter;
import com.element.music.service.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述：
 * <p>
 * User: nft musician   developer
 * Date: 2022/1/10
 */

@Service
@Transactional
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Value("${user.profile-img-path}")
    private String userProfileImgPath;


    @Value("${user.backdrop}")
    private String userBackDropPath;

    @Override
    public List<UserDTO> getUserList() {
        List<UserEntity> all = userRepository.findAll();
        return all.stream().map(UserConverter::entity2Dto ).collect(Collectors.toList());
    }

    @Override
    public UserIdVO addUser(String walletAddress) {
        UserEntity userEntity = new UserEntity();
        userEntity.setWalletAddress(walletAddress);
        UserEntity save = userRepository.save(userEntity);
        return UserIdVO.of().setUserId(save.getId());
    }

    @Override
    public Boolean delUserByWalletAddress(String walletAddress) {
        userRepository.deleteByWalletAddress(walletAddress);
        return Boolean.TRUE;
    }

    @Override
    public UserDTO getUserById(Long userId) {
        UserEntity one = userRepository.getOne(userId);
        return UserConverter.entity2Dto(one);
    }

    @Override
    public UserDTO getUserByWalletAddress(String walletAddress) {
        UserEntity userEntityByWalletAddress = userRepository.findUserEntityByWalletAddress(walletAddress);
        return UserConverter.entity2Dto(userEntityByWalletAddress);
    }

    @Override
    public Boolean updateUserName(Long userId, String userName) {
        userRepository.updateUserName(userId, userName);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateUserEmail(Long userId, String email) {
        userRepository.updateUserEmail(userId, email);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateUserProfileImg(Long userId, MultipartFile profileImg) {
        String picUrl = FileUtils.uploadFile(userProfileImgPath, profileImg);
        userRepository.updateUserProfileImg(userId, picUrl);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateUserBackDrop(Long userId, MultipartFile backDrop) {
        String picUrl = FileUtils.uploadFile(userBackDropPath, backDrop);
        userRepository.updateBackDrop(userId, picUrl);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateUserTwitter(Long userId, String twitter) {
        userRepository.updateUserTwitter(userId, twitter);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateUserWebsite(Long userId, String website) {
        userRepository.updateUserWebsite(userId, website);
        return Boolean.TRUE;
    }

    @Override
    public UserDTO login(String walletAddress) {
        UserEntity userEntityByWalletAddress = userRepository.findUserEntityByWalletAddress(walletAddress);
        //TODO 生成sessionId
        //返回用户详情
        return UserConverter.entity2Dto(userEntityByWalletAddress);
    }

    @Override
    public void logout(String walletAddress) {
        userRepository.clearSessionId(walletAddress);
    }
}
